package com.ly.mssp.license.client.registry;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;

public class ArchiveClient {

	private OutputStream out=null;
	
	TarArchiveOutputStream tarout=null;	
	
	private String fileName;
	
	private String imageName;
	
	private String path;
	
	public ArchiveClient(OutputStream out, String imageName) {
		this.out=out;		
		this.imageName=imageName;
	    this.init();
	}
	
	public String getFileName() {
		return fileName;
	}
	
	private void init() {
		
		this.tarout=new TarArchiveOutputStream(this.out);
		
//		 File f=new File("");
//		 String path = f.getAbsolutePath();
//		 
//		 String id=UUID.randomUUID().toString().replace("-", "")+
//			UUID.randomUUID().toString().replace("-", "");
//					
//		 try {
//			
//			path=path+File.separator+id;			
//			
//			File dir=new File(path);
//			dir.mkdirs();
//			
//			this.path=path;
//			
//			this.fileName=path+File.separator+this.imageName+".tar";
//			
//			FileOutputStream out=new FileOutputStream(new File(this.fileName));
//			
//			this.tarout=new TarArchiveOutputStream(out);
//			
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

	/**
	 * 
	 * 压缩文件
	 * 
	 * @param params
	 * @throws Exception 
	 */
	public void compress(Map<String, byte[]> params) throws Exception {
		
		this.tarout=new TarArchiveOutputStream(out);
		
		try {
			Set<Entry<String, byte[]>> dataEntrys = params.entrySet();
			
			for (Entry<String, byte[]> data : dataEntrys) {
				byte[] buf=data.getValue();
				try {
					try {										
						TarArchiveEntry entry=new TarArchiveEntry(data.getKey());
						entry.setSize(buf.length);
						
						tarout.putArchiveEntry(entry);
						
						tarout.write(buf);
//						
						tarout.closeArchiveEntry();
						
						tarout.flush();
						
					}finally {
						//in.close();
					}
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			tarout.finish();
			
		}finally {
			tarout.close();			
		}
	}

	public void compress(String key, InputStream in) {		
		try {
			BufferedInputStream stream = new BufferedInputStream(in);
			try {				
				byte[] bs = new byte[1024]; 
				
				TarArchiveEntry entry=new TarArchiveEntry(key);
				entry.setSize(in.available());
				
				tarout.putArchiveEntry(entry);
										
				int len=-1;
				while ((len=in.read(bs, 0, 1024))!=-1) {
					tarout.write(bs, 0, len);
				}
				
				tarout.closeArchiveEntry();
				
			}finally {
				stream.close();
			}
		}catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	public void compress(String key, byte[] buffer) {
		try {			
			
			TarArchiveEntry entry=new TarArchiveEntry(key);
			entry.setSize(buffer.length);
			
			tarout.putArchiveEntry(entry);
			
			tarout.write(buffer, 0, buffer.length);
			
			tarout.closeArchiveEntry();				
		
		}catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	public void flush() throws Exception{
		if (this.tarout!=null) {
			this.tarout.flush();
			this.tarout.finish();
		}
	}
	
	public void close() throws Exception{
		if (this.tarout!=null) {
			this.tarout.close();
		}
	}
}
